package com.xyp.homework;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/8/5
 * @Desc 从想做到能做
 */
class MyThread {
    public static int flag = 0;
    public static int count = 1;
    static Object lock = new Object();
}
class childThread implements Runnable {
    @Override
    public void run() {
        while (true) {
            synchronized (MyThread.lock) {
                if (MyThread.count > 10) {
                    break;
                } else {
                    if (MyThread.flag == 0) {
                        try {
                            MyThread.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        for (int i = 1; i <= 2; i++) {
                            System.out.println("子线程正在打印第" + i + "次");
                        }
                        System.out.println("=================================");
                        MyThread.flag = 0;
                        MyThread.count++;
                        MyThread.lock.notifyAll();
                    }
                }
            }
        }
    }
}
public class Demo2 {
    public static void main(String[] args) {

        new Thread(new childThread()).start();

        while (true) {
            synchronized (MyThread.lock) {
                if (MyThread.count > 10) {
                    break;
                } else {
                    if (MyThread.flag == 1) {
                        try {
                            MyThread.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        for (int i = 1; i <= 2; i++) {
                            System.out.println("主线程正在打印第" + i + "次");
                        }
                        MyThread.flag = 1;
                        MyThread.lock.notifyAll();
                    }
                }
            }
        }
    }
}